package com.uobgroup.ews.uam.core.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.uobgroup.ews.uam.core.dao.ApplicationConfigurationDao;
import com.uobgroup.ews.uam.core.dao.UAMUserDao;
import com.uobgroup.ews.uam.core.domain.UAMUser;
import com.uobgroup.ews.uam.core.model.NotifyLoginResult;
import com.uobgroup.ews.uam.core.model.VerifyLoginResult;

/*
 * Phuong Tran
 */

@Service
@Transactional
public class LoginAttemptServiceImpl implements LoginAttemptService {

   @Autowired
   UAMUserDao uamUserDao;

   @Autowired
   ApplicationConfigurationDao applicationConfigurationDao;

   private static final Integer RESET_ATTEMPT_VALUE = 0;

   @Override
   public VerifyLoginResult verifyLoginAllow(String user, String workStation) {
      UAMUser uamUser = checkUser(user);
      if (uamUser == null) {
         return new VerifyLoginResult(false);
      }
      else {
         if (uamUser.getIsLocked() || uamUser.getIsDeleted() || !uamUser.getIsActive()) {
            return new VerifyLoginResult(false);
         }
         else {
            Integer attemptNum = uamUser.getAttemptNum();
            if (attemptNum == null)
               attemptNum = 0;
            if (attemptNum >= applicationConfigurationDao.getMaxLoginAttempt()) {
               uamUser.setIsLocked(true);
               uamUserDao.update(uamUser);
               return new VerifyLoginResult(false);
            }
            else {
               attemptNum++;
               uamUser.setAttemptNum(attemptNum);
               uamUser.setLastLogonDate(new Date());
               uamUserDao.update(uamUser);
               return new VerifyLoginResult(true);
            }
         }
      }
   }

   @Override
   public NotifyLoginResult notifyLoginSuccess(String user, String workStation) {
      UAMUser uamUser = checkUser(user);
      if (uamUser == null) {
         return new NotifyLoginResult(false, new Date().toString());
      }
      else {
         Date lastLogonDate = new Date();
         if (uamUser.getLastLogonDate() != null) {
            lastLogonDate = uamUser.getLastLogonDate();
         }
         if (uamUser.getIsLocked() || uamUser.getIsDeleted() || !uamUser.getIsActive()) {
            return new NotifyLoginResult(false, lastLogonDate.toString());
         }
         else {
            uamUser.setAttemptNum(RESET_ATTEMPT_VALUE);
            uamUserDao.update(uamUser);
            return new NotifyLoginResult(true, lastLogonDate.toString());
         }
      }
   }

   private UAMUser checkUser(String user) {
      UAMUser retUser = null;
      String[] userArr = user.split("@");
      String userName = userArr[0];
      String userDomain = userArr[1];
      List<UAMUser> lstUser = uamUserDao.findUsersByName(userName);
      Iterator<UAMUser> itUser = lstUser.iterator();
      while (itUser.hasNext()) {
         UAMUser uamUser = itUser.next();
         String domain = uamUser.getDomain().getPropertyValue();
         if (userDomain.equals(domain)) {
            retUser = uamUser;
            break;
         }
      }
      return retUser;
   }

}
